import Mock from 'mockjs'
import qs from 'qs'

const baseUrl = process.env.VUE_APP_BASE_API

export default function ajaxMock(suffixUrl, mockFun) {
  Mock.mock(new RegExp('^' + baseUrl + suffixUrl), (options) => {
    // 解析ajax发送过来的data
    if (/^get$/i.test(options.type)) {
      let url = options.url
      url = url.replace(new RegExp(`^${baseUrl}${suffixUrl}\\??&?`), '')
      options.data = url ? qs.parse(url) : null
    } else {
      options.data = options.body ? qs.parse(options.body) : null
    }
    return mockFun(options)
  })
}
// options 对象的参数
/*
  public url: string
  public type: string
  public body: string // body只有在post下才有值
  public data: object | null // 拦截的数据
*/
